﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Phone
{
    public partial class Text : Form
    {
        public Text()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string message = textBox1.Text;
            string remoteMessageNumber = "";
            switch (Vars.busy_sip_or_pstn)
            {
                case "sip":
                    remoteMessageNumber = Vars.sip.GetRemoteUser();
                    break;
                case "pstn1":
                    remoteMessageNumber = Vars.pstn1.GetRemoteUser();
                    break;
                case "pstn2":
                    remoteMessageNumber = Vars.pstn2.GetRemoteUser();
                    break;
            }
            switch (remoteMessageNumber)
            {
                case "110":
                    remoteMessageNumber = Defines.RECEIVE_MSG_NUMBER_110;
                    break;
                case "119":
                    remoteMessageNumber = Defines.RECEIVE_MSG_NUMBER_119;
                    break;
                case "120":
                    remoteMessageNumber = Defines.RECEIVE_MSG_NUMBER_120;
                    break;

                case "122":
                    remoteMessageNumber = Defines.RECEIVE_MSG_NUMBER_122;
                    break;
            }
            if (Vars.tag == "") return;
            switch (Vars.busy_sip_or_pstn)
            {
                case "sip":
                    Vars.sip?.SendMsg(remoteMessageNumber, "flag:Tag=" + Vars.tag + ";Chat=" + message+";");
                    break;
                case "pstn1":
                    Vars.pstn1?.SendMsg(remoteMessageNumber, "flag:Tag=" + Vars.tag + ";Chat=" + message+";");
                    break;
                case "pstn2":
                    Vars.pstn2?.SendMsg(remoteMessageNumber, "flag:Tag=" + Vars.tag + ";Chat=" + message+";");
                    break;
            }
            Vars.chatMessage += "我:"+message + "\n";
            listBox1.Items.Add("我:"+message);
            textBox1.Text = "";
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Talking talking = Application.OpenForms.OfType<Talking>().FirstOrDefault();
            talking.Show();
            talking.Location = this.Location;
            this.Close();
        }

        private void Text_Load(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
            listBox1.Refresh();
            Vars.chatMessage.Split('\n').ToList().ForEach(x => listBox1.Items.Add(x));
        }

        public void AddMessage(string message)
        {
            if (listBox1.InvokeRequired)
            {
                Invoke(new Action(() => listBox1.Items.Add(message)));
            }
            else listBox1.Items.Add(message);
        }
    }
}
